import java.io.*;

public class ReaderAndWriter {
    public static void main(String[] args) throws IOException {
        //将对象的构造放入try代码块中，当程序无论正常运行与否都可以正常关闭资源
        try(Reader reader = new FileReader("E:/codeforbit/IO/test1.txt")){
            while(true){
                char[] cbuf = new char[1024];
                int n = reader.read(cbuf);  //read方法的返回值为int类型，当文件为空时返回-1
                if(n == -1){
                    break;
                }
                System.out.println("n= " + n);
                for(int i = 0; i < n; i++){
                    System.out.println(cbuf[i]);
                }
            }
        }
        try(Writer writer = new FileWriter("E:/codeforbit/IO/test1.txt", true) ){
            writer.write("你好");
        }
    }
}
